const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    list: [],
    nav: [{
      title: '投票中',
      id: 2,
    }, {
      title: '待开始',
      id: 1,
    }, {
      title: '已结束',
      id: 3,
    }],
    openType: '',
    votestatus: 2, // 1-待开始，2-投票中，3-已结束
    hasMore: true,
  },
  onLoad(ops) {
    wx.setNavigationBarTitle({
      title: '投票调查',
    });
  },
  onShow() {
    this.setData({
      page: 1,
    }, () => {
      this.loadList();
    });
  },
  changeNav(e) {
    this.setData({
      page: 1,
      votestatus: e.detail.obj.id,
      list: [],
      isLoad: false,
    }, () => {
      this.loadList();
    });
  },
  loadList() {
    const {
      page,
      votestatus,
      list: lists,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/partyman.vote.getVoteList',
      data: {
        page,
        votestatus,
      },
      success: (res) => {
        let list = res.list;
        if (page > 1) {
          list = [...lists, ...list];
        }
        this.setData({
          hasMore: res.list.length !== 0,
          list,
          isLoad: true,
        });
      },
    });
  },
  onReachBottom() {
    if (!this.data.hasMore) return;
    this.setData({
      page: this.data.page + 1,
    }, () => {
      this.loadList();
    });
  },
  goVodeDetail(e) {
    let { id, title, isresult, hasvote } = e.currentTarget.dataset;

    if (this.data.votestatus == 3) {
      hasvote = 1;
    }

    if (isresult == 1 || hasvote == 0) {
      wx.navigateTo({
        url: `/ws_party_const/pages/vote/detail?id=${id}&barTitle=${title}&isresult=${isresult}`,
      });
    } else {
      wx.showToast({
        title: '该投票结果不可查看',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
    }

  },
});